package tools

import (
	"fmt"
	"github.com/sony/sonyflake"
	"sync"
)

var (
	singleton *Unique
	once      sync.Once
)

type Unique struct {
	flake *sonyflake.Sonyflake
}

func newSnowflake() *Unique {
	flake := sonyflake.NewSonyflake(sonyflake.Settings{})

	return &Unique{
		flake: flake,
	}
}

func GetUniqueInstance() *Unique {
	once.Do(func() {
		singleton = newSnowflake()
	})
	return singleton
}

func (u *Unique) NextID() (uint64, error) {
	if u == nil || u.flake == nil {
		return 0, fmt.Errorf("unique instance is not initialized")
	}

	return u.flake.NextID()
}
